package cn.com.headfree.mouldshow.dao.impl;

import cn.com.headfree.entity.TPerformanceScoreTemp;
import cn.com.headfree.mouldshow.dao.TPerformanceScoreTempDao;
import cn.com.headfree.mouldshow.vo.TPerformanceScoreVo;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Repository("/tPerformanceScoreTempDao")
public class TPerformanceScoreTempDaoImpl extends BaseDaoImpl<TPerformanceScoreTemp> implements TPerformanceScoreTempDao {


    @Override
    public void saveInfo(TPerformanceScoreTemp tPerformanceScoreTemp) {
        String sql = "insert into t_performance_score_temp (id,erp_no,user_name,year,salary_grade,salary_level,perfor_grade,remain_point,creater_id,creater_name,create_time,updater_id,updater_name,update_time,del_sign) values ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append("(#{id,jdbcType=VARCHAR},").append("#{erp_no,jdbcType=VARCHAR},")
                .append("#{user_name,jdbcType=VARCHAR},").append("#{year,jdbcType=INTEGER},")
                .append("#{salary_grade,jdbcType=VARCHAR},").append("#{salary_level,jdbcType=INTEGER},")
                .append("#{perfor_grade,jdbcType=VARCHAR},").append("#{remain_point,jdbcType=DOUBLE},")
                .append("#{creater_id,jdbcType=VARCHAR},").append("#{creater_name,jdbcType=VARCHAR},")
                .append("#{create_time,jdbcType=DATE},").append("#{updater_id,jdbcType=VARCHAR},")
                .append("#{updater_name,jdbcType=VARCHAR},").append("#{update_time,jdbcType=DATE},")
                .append("#{del_sign,jdbcType=INTEGER})");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", tPerformanceScoreTemp.getId());
        params.put("erp_no", tPerformanceScoreTemp.getErpNo());
        params.put("user_name", tPerformanceScoreTemp.getUserName());
        params.put("year", tPerformanceScoreTemp.getYear());
        params.put("salary_grade", tPerformanceScoreTemp.getSalaryGrade());
        params.put("salary_level", tPerformanceScoreTemp.getSalaryLevel());
        params.put("perfor_grade", tPerformanceScoreTemp.getPerforGrade());
        params.put("remain_point", tPerformanceScoreTemp.getRemainPoint());
        params.put("creater_id", tPerformanceScoreTemp.getCreaterId());
        params.put("creater_name", tPerformanceScoreTemp.getCreaterName());
        params.put("create_time", new Timestamp(new Date().getTime()));
        params.put("updater_id", tPerformanceScoreTemp.getUpdaterId());
        params.put("updater_name", tPerformanceScoreTemp.getUpdaterName());
        params.put("update_time", new Timestamp(new Date().getTime()));
        params.put("del_sign",  0);

        this.executeSql(sqlBuilder.toString(), params);
    }

    @Override
    public void searchPerformanceScoreList(Page<TPerformanceScoreVo> page, TPerformanceScoreVo tPerformanceScoreVo){
        ArrayList<Object> arrayList = new ArrayList<>();
        StringBuilder sqlbuilder = new StringBuilder(" SELECT a.id,a.erp_no,a.user_name,a.rate_year as year,a.perfor_grade,a.perfor_grade," +
                "b.salary_grade,b.remain_point,d.id AS dept_id,d.NAME AS dept_name,c.org_id AS baseorg_id " +
                " FROM t_year_effect_info a left join t_person_year_point b on a.erp_no=b.erp_no and a.rate_year=b.year+1 and b.del_sign=0 LEFT JOIN t_dept_emp_rela c ON a.erp_no = c.erp_no " +
                "LEFT JOIN t_org_dept d ON d.id = c.dept_id WHERE a.del_sign = 0 AND a.is_valid = 0 and a.is_cover=0 ");
        if (null != tPerformanceScoreVo) {
            //erp员工号
            if (!StringUtil.isNullOrEmpty(tPerformanceScoreVo.getErpNo())) {
                sqlbuilder.append(" AND a.erp_no LIKE concat('%',?,'%') ESCAPE '/'");
                String replaceStr = tPerformanceScoreVo.getErpNo().replaceAll("%", "/%").replaceAll("_", "/_");
                arrayList.add(replaceStr);
            }
            //人员名称
            if (!StringUtil.isNullOrEmpty(tPerformanceScoreVo.getUserName())) {
                sqlbuilder.append(" AND a.user_name LIKE concat('%',?,'%') ESCAPE '/'");
                String replaceStr = tPerformanceScoreVo.getUserName().replaceAll("%", "/%").replaceAll("_", "/_");
                arrayList.add(replaceStr);
            }
            //年份
            if (null != tPerformanceScoreVo.getYear()) {
                sqlbuilder.append(" AND a.rate_year = ?");
                arrayList.add(tPerformanceScoreVo.getYear());
            }
            if (!StringUtil.isNullOrEmpty(tPerformanceScoreVo.getBaseorgId())) {
                sqlbuilder.append(" AND d.path LIKE concat('%',?,'%') ");
                arrayList.add(tPerformanceScoreVo.getBaseorgId());
            }
        }
        sqlbuilder.append(" ORDER BY a.rate_year desc,a.create_time desc");
        this.findPageBySql(TPerformanceScoreVo.class, sqlbuilder.toString(), page, arrayList.toArray());
    }
}
